package com.epalmpay.controller.channel.datareport;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.epalmpay.bean.TradeCheckOpBean;
import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.datareport.TradeCheckedInDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.trade.DeviceOfflineRecordInDTO;
import com.epalmpay.dto.trade.DeviceOfflineRecordListOutDTO;
import com.epalmpay.dto.trade.DeviceOfflineRecordOutDTO;
import com.epalmpay.dto.trade.TradeCheckInDTO;
import com.epalmpay.dto.trade.TradeCheckOpOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.entity.TradeOfflineAbandon;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.trade.IDevOfflineRecordService;
import com.epalmpay.service.trade.ITradeCheckService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.SysUtils;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/merchant/tradeCheck")
public class TradeCheckController  extends BaseController{
	    
	@Resource
    private IDevOfflineRecordService devOfflineRecordService;
	@Resource
    private ITradeCheckService tradeCheckService;
	
	@Resource
	private IMemberService memberService;
	   /**
     * 显示线下交易记录分页列表
     * @param offlineRecordInDTO
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/offlineRecordList")
    @ResponseBody
    public DeviceOfflineRecordListOutDTO listOfflineRecord(TradeCheckedInDTO offlineRecordInDTO, PageDTO pageDTO){
        DeviceOfflineRecordListOutDTO offlineRecordListOutDTO = new DeviceOfflineRecordListOutDTO();
        ArrayList<DeviceOfflineRecordOutDTO> offlineRecordOutDTOs=new ArrayList<DeviceOfflineRecordOutDTO>();
        try {

            Page page= Tools.pageEmtity(pageDTO);

            List<TradeOffline> list=devOfflineRecordService.listCheckOfflineRecords(offlineRecordInDTO, page);
            for (int i = 0; i < list.size(); i++) {
                DeviceOfflineRecordOutDTO dtos =new DeviceOfflineRecordOutDTO();
//                PropertyCopyUtil.copyPropertiesExclude(list.get(i),dtos);

                dtos.setId(list.get(i).getId());
                dtos.setGmtCreate(list.get(i).getGmtCreate());
                dtos.setGmtModified(list.get(i).getGmtModified());
                dtos.setDiscountAmount(list.get(i).getDiscountAmount()+list.get(i).getBonusValue());//优惠金额 = 折扣+ 使用优惠券的
                dtos.setGroupId(list.get(i).getGroupId());
                dtos.setOrderAmount(list.get(i).getOrderAmount());
                dtos.setOrderId(list.get(i).getOrderId());
                dtos.setPayerId(list.get(i).getPayerId());
//                dtos.setPayerName(list.get(i).getPayerName());
                if (list.get(i).getPayType() != null) {
                    dtos.setPayType(list.get(i).getPayType());
                }
                dtos.setRealAmount(list.get(i).getRealAmount());
                dtos.setReceiverId(list.get(i).getReceiverId());
                dtos.setReceiverName(list.get(i).getReceiverName());
                dtos.setTransactionStatus(list.get(i).getTransactionStatus());
                dtos.setTransactionType(list.get(i).getTransactionType());
                dtos.setFee(list.get(i).getFee());
                dtos.setBonusAmount(list.get(i).getBonusAmount());
//                dtos.setBonusValue(list.get(i).getBonusValue());//去掉 直接加到优惠金额字段里了 方便做导出
                if (list.get(i).getTransactionType() != null && list.get(i).getTransactionType() == 15){
                    dtos.setPayerName(offlineRecordInDTO.getGroupName());
                }else{
                    dtos.setPayerName(list.get(i).getPayerName());
                }
                dtos.setCheckStatus(list.get(i).getCheckStatus());
                offlineRecordOutDTOs.add(dtos);
                offlineRecordListOutDTO.setDeviceOfflineRecordOutDTOList(offlineRecordOutDTOs);
            }
            offlineRecordListOutDTO.setPage(page);
            offlineRecordListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return offlineRecordListOutDTO;

        } catch (ApplicationException ae) {
            offlineRecordListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            offlineRecordListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return offlineRecordListOutDTO;
    }
    
    
	   /**
  * 显示线下交易记录分页列表
  * @param offlineRecordInDTO
  * @param pageDTO
  * @return
  */
 @RequestMapping(value = "/abandonOfflineRecordList")
 @ResponseBody
 public DeviceOfflineRecordListOutDTO abandonOfflineRecordList(TradeCheckedInDTO offlineRecordInDTO, PageDTO pageDTO){
     DeviceOfflineRecordListOutDTO offlineRecordListOutDTO = new DeviceOfflineRecordListOutDTO();
     ArrayList<DeviceOfflineRecordOutDTO> offlineRecordOutDTOs=new ArrayList<DeviceOfflineRecordOutDTO>();
     try {

         Page page= Tools.pageEmtity(pageDTO);

         List<TradeOfflineAbandon> list=tradeCheckService.listOfflineRecords(offlineRecordInDTO, page);
         for (int i = 0; i < list.size(); i++) {
             DeviceOfflineRecordOutDTO dtos =new DeviceOfflineRecordOutDTO();
//             PropertyCopyUtil.copyPropertiesExclude(list.get(i),dtos);

             dtos.setId(list.get(i).getId());
             dtos.setGmtCreate(list.get(i).getGmtCreate());
             dtos.setGmtModified(list.get(i).getGmtModified());
             dtos.setDiscountAmount(list.get(i).getDiscountAmount()+list.get(i).getBonusValue());//优惠金额 = 折扣+ 使用优惠券的
             dtos.setGroupId(list.get(i).getGroupId());
             dtos.setOrderAmount(list.get(i).getOrderAmount());
             dtos.setOrderId(list.get(i).getOrderId());
             dtos.setPayerId(list.get(i).getPayerId());
//             dtos.setPayerName(list.get(i).getPayerName());
             if (list.get(i).getPayType() != null) {
                 dtos.setPayType(list.get(i).getPayType());
             }
             dtos.setRealAmount(list.get(i).getRealAmount());
             dtos.setReceiverId(list.get(i).getReceiverId());
             dtos.setReceiverName(list.get(i).getReceiverName());
             dtos.setTransactionStatus(list.get(i).getTransactionStatus());
             dtos.setTransactionType(list.get(i).getTransactionType());
             dtos.setFee(list.get(i).getFee());
             dtos.setBonusAmount(list.get(i).getBonusAmount());
//             dtos.setBonusValue(list.get(i).getBonusValue());//去掉 直接加到优惠金额字段里了 方便做导出
             if (list.get(i).getTransactionType() != null && list.get(i).getTransactionType() == 15){
                 dtos.setPayerName(offlineRecordInDTO.getGroupName());
             }else{
                 dtos.setPayerName(list.get(i).getPayerName());
             }
             dtos.setCheckStatus(list.get(i).getCheckStatus());
             offlineRecordOutDTOs.add(dtos);
             offlineRecordListOutDTO.setDeviceOfflineRecordOutDTOList(offlineRecordOutDTOs);
         }
         offlineRecordListOutDTO.setPage(page);
         offlineRecordListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
         return offlineRecordListOutDTO;

     } catch (ApplicationException ae) {
         offlineRecordListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
         ae.printStackTrace();
     }
     catch (Exception e) {
         offlineRecordListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
         e.printStackTrace();
     }
     return offlineRecordListOutDTO;
 }
    
    //接收对账文件
    //String[] columHeader = {"通道标识", "交易商户号", "商户名称", "交易终端号","交易流水号", "交易批次号","交易参考号","交易金额" ,"结算金额" ,"交易结果", "交易类型", "交易时间","设备序列号","交易状态值","交易类型值" };
	
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path, @RequestParam("verify") String verify) {
    	String inStr = "zhangfuJYLS"+path;
		String sign = SysUtils.string2MD5(inStr);
		if(!sign.equalsIgnoreCase(verify)){
			return "签名错误";
		}
    	
        String realPath;
		try {
			realPath = tradeCheckService.finishSaveTradeCheckFile( file);
		} catch (Exception e) {
			
			e.printStackTrace();
			realPath=e.getMessage();
		}
       
        return realPath;
    }
    
   
    
    /**
     * 通过   
    /**
     * 通过用户id查询交易详情
     *
     */
    @RequestMapping(value="/offlineRecordFindById")
    @ResponseBody
    public DeviceOfflineRecordOutDTO  findByOfflineRecord(DeviceOfflineRecordInDTO deviceOfflineRecordInDTO){
        DeviceOfflineRecordOutDTO deviceOfflineRecordOutDTO=new DeviceOfflineRecordOutDTO();
        TradeOffline offline=new TradeOffline();
        try {
            offline.setId(deviceOfflineRecordInDTO.getId());
            offline.setMemberId(deviceOfflineRecordInDTO.getMemberId());
            TradeOffline offlineRecord=tradeCheckService.offlinefindById(offline);
            if(offlineRecord!=null){
                deviceOfflineRecordOutDTO.setId(offlineRecord.getId());
                deviceOfflineRecordOutDTO.setGmtCreate(offlineRecord.getGmtCreate());
                deviceOfflineRecordOutDTO.setGmtModified(offlineRecord.getGmtModified());
                deviceOfflineRecordOutDTO.setDiscountAmount(offlineRecord.getDiscountAmount());
                deviceOfflineRecordOutDTO.setGroupId(offlineRecord.getGroupId());
                deviceOfflineRecordOutDTO.setOrderAmount(offlineRecord.getOrderAmount());
                deviceOfflineRecordOutDTO.setOrderId(offlineRecord.getOrderId());
                deviceOfflineRecordOutDTO.setPayerId(offlineRecord.getPayerId());
                deviceOfflineRecordOutDTO.setPayerName(offlineRecord.getPayerName());
                deviceOfflineRecordOutDTO.setPayType(offlineRecord.getPayType());
                deviceOfflineRecordOutDTO.setRealAmount(offlineRecord.getRealAmount());
                deviceOfflineRecordOutDTO.setReceiverId(offlineRecord.getReceiverId());
                deviceOfflineRecordOutDTO.setReceiverName(offlineRecord.getReceiverName());
                deviceOfflineRecordOutDTO.setTransactionStatus(offlineRecord.getTransactionStatus());
                deviceOfflineRecordOutDTO.setTransactionType(offlineRecord.getTransactionType());
                deviceOfflineRecordOutDTO.setMerchantNo(offlineRecord.getMerchantNo());
                deviceOfflineRecordOutDTO.setTerminalNo(offlineRecord.getTerminalNo());
                deviceOfflineRecordOutDTO.setTermSerial(offlineRecord.getTermSerial());
                deviceOfflineRecordOutDTO.setMemberId(offlineRecord.getMemberId());
                deviceOfflineRecordOutDTO.setMemberName(offlineRecord.getMemberName());
                deviceOfflineRecordOutDTO.setGradeName(offlineRecord.getGradeName());
                deviceOfflineRecordOutDTO.setFromName(offlineRecord.getFromName());
                deviceOfflineRecordOutDTO.setCouponCount(offlineRecord.getCouponCount());
                deviceOfflineRecordOutDTO.setCouponInfo(offlineRecord.getCouponInfo());
                deviceOfflineRecordOutDTO.setAdjustAmount(offlineRecord.getAdjustAmount());
                deviceOfflineRecordOutDTO.setBackAmount(offlineRecord.getBackAmount());
                deviceOfflineRecordOutDTO.setBonusAmount(offlineRecord.getBonusAmount());
                deviceOfflineRecordOutDTO.setBonusValue(offlineRecord.getBonusValue());
                deviceOfflineRecordOutDTO.setCardNo(offlineRecord.getCardNo());
//                PropertyCopyUtil.copyPropertiesExclude(offlineRecord,deviceOfflineRecordOutDTO);
                deviceOfflineRecordOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
                return deviceOfflineRecordOutDTO;
            }else{
                deviceOfflineRecordOutDTO.setErrorCode("-1", "未查询到记录");
            }
        } catch (Exception e) {
            deviceOfflineRecordOutDTO.setErrorCode("-1", "处理出现异常");
            e.printStackTrace();
        }
        deviceOfflineRecordOutDTO.setErrorCode("-2", "处理失败");
        return deviceOfflineRecordOutDTO;
    }
    /**id查询交易详情
     *
     */
    @RequestMapping(value="/abandonOfflineRecordFindById")
    @ResponseBody
    public DeviceOfflineRecordOutDTO  abandonOfflineRecordFindById(DeviceOfflineRecordInDTO deviceOfflineRecordInDTO){
        DeviceOfflineRecordOutDTO deviceOfflineRecordOutDTO=new DeviceOfflineRecordOutDTO();
        TradeOffline offline=new TradeOffline();
        try {
            offline.setId(deviceOfflineRecordInDTO.getId());
            offline.setMemberId(deviceOfflineRecordInDTO.getMemberId());
            TradeOffline offlineRecord=tradeCheckService. abandonOfflinefindById(offline);
            if(offlineRecord!=null){
                deviceOfflineRecordOutDTO.setId(offlineRecord.getId());
                deviceOfflineRecordOutDTO.setGmtCreate(offlineRecord.getGmtCreate());
                deviceOfflineRecordOutDTO.setGmtModified(offlineRecord.getGmtModified());
                deviceOfflineRecordOutDTO.setDiscountAmount(offlineRecord.getDiscountAmount());
                deviceOfflineRecordOutDTO.setGroupId(offlineRecord.getGroupId());
                deviceOfflineRecordOutDTO.setOrderAmount(offlineRecord.getOrderAmount());
                deviceOfflineRecordOutDTO.setOrderId(offlineRecord.getOrderId());
                deviceOfflineRecordOutDTO.setPayerId(offlineRecord.getPayerId());
                deviceOfflineRecordOutDTO.setPayerName(offlineRecord.getPayerName());
                deviceOfflineRecordOutDTO.setPayType(offlineRecord.getPayType());
                deviceOfflineRecordOutDTO.setRealAmount(offlineRecord.getRealAmount());
                deviceOfflineRecordOutDTO.setReceiverId(offlineRecord.getReceiverId());
                deviceOfflineRecordOutDTO.setReceiverName(offlineRecord.getReceiverName());
                deviceOfflineRecordOutDTO.setTransactionStatus(offlineRecord.getTransactionStatus());
                deviceOfflineRecordOutDTO.setTransactionType(offlineRecord.getTransactionType());
                deviceOfflineRecordOutDTO.setMerchantNo(offlineRecord.getMerchantNo());
                deviceOfflineRecordOutDTO.setTerminalNo(offlineRecord.getTerminalNo());
                deviceOfflineRecordOutDTO.setTermSerial(offlineRecord.getTermSerial());
                deviceOfflineRecordOutDTO.setMemberId(offlineRecord.getMemberId());
                deviceOfflineRecordOutDTO.setMemberName(offlineRecord.getMemberName());
                deviceOfflineRecordOutDTO.setGradeName(offlineRecord.getGradeName());
                deviceOfflineRecordOutDTO.setFromName(offlineRecord.getFromName());
                deviceOfflineRecordOutDTO.setCouponCount(offlineRecord.getCouponCount());
                deviceOfflineRecordOutDTO.setCouponInfo(offlineRecord.getCouponInfo());
                deviceOfflineRecordOutDTO.setAdjustAmount(offlineRecord.getAdjustAmount());
                deviceOfflineRecordOutDTO.setBackAmount(offlineRecord.getBackAmount());
                deviceOfflineRecordOutDTO.setBonusAmount(offlineRecord.getBonusAmount());
                deviceOfflineRecordOutDTO.setBonusValue(offlineRecord.getBonusValue());
                deviceOfflineRecordOutDTO.setCardNo(offlineRecord.getCardNo());
//                PropertyCopyUtil.copyPropertiesExclude(offlineRecord,deviceOfflineRecordOutDTO);
                deviceOfflineRecordOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功");
                return deviceOfflineRecordOutDTO;
            }else{
                deviceOfflineRecordOutDTO.setErrorCode("-1", "未查询到记录");
            }
        } catch (Exception e) {
            deviceOfflineRecordOutDTO.setErrorCode("-1", "处理出现异常");
            e.printStackTrace();
        }
        deviceOfflineRecordOutDTO.setErrorCode("-2", "处理失败");
        return deviceOfflineRecordOutDTO;
    }
    
    
    
    
  	
    @RequestMapping(value = "addToTradeRecord", method = RequestMethod.POST)
    @ResponseBody
    public BaseOutDTO addToTradeRecord(TradeCheckInDTO tradeCheckInDTo) {
    	BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			//
			tradeCheckService.finishAddToTradeRecord(tradeCheckInDTo);
		
			baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return baseOutDTO;
		}
		catch (ApplicationException ae) { 
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
    } 
    
    @RequestMapping(value = "abandonTradeRecord", method = RequestMethod.POST)
    @ResponseBody
    public BaseOutDTO abandonTradeRecord(TradeCheckInDTO tradeCheckInDTo) {
    	BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			//
			tradeCheckService.finishAbandonTradeRecord(tradeCheckInDTo);
		
			baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return baseOutDTO;
		}
		catch (ApplicationException ae) { 
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
    }
    
    @RequestMapping(value = "tradeCheckOpRecordList", method = RequestMethod.POST)
    @ResponseBody
    public TradeCheckOpOutDTO tradeCheckOpRecord(TradeCheckInDTO tradeCheckInDTo , PageDTO pageDTO) {
    	TradeCheckOpOutDTO tradeCheckOpOutDTO = new TradeCheckOpOutDTO();
		try {
			//查询营销账户余额
		   Page page= Tools.pageEmtity(pageDTO);
			List<TradeCheckOpBean> tradeCheckOpList=tradeCheckService.tradeCheckOpRecordList(tradeCheckInDTo,page);
			tradeCheckOpOutDTO.setTradeCheckOpList(tradeCheckOpList);
			tradeCheckOpOutDTO.setPage(page);
			tradeCheckOpOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return tradeCheckOpOutDTO;
		}
		catch (ApplicationException ae) { 
			tradeCheckOpOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 tradeCheckOpOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return tradeCheckOpOutDTO;
    }
    
    
}
